               package com.sun.app.service.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import sun.core.json.JSONArray;
import sun.core.json.JSONObject;

import com.sun.app.service.BaseService;
import com.sun.core.Constants;
import com.sun.core.common.PagingList;
import com.sun.dao.CFriendMapper;
import com.sun.dao.CUserMapper;
import com.sun.util.AppResult;
import com.sun.util.TimeUtil;
import com.sun.util.limit.CompareUtil;
import com.sun.util.limit.LimitType;
import com.sun.vo.CFriend;
import com.sun.vo.CFriendExample;
import comet.vo.Host;

public class FriendService extends BaseService {

	@Autowired
	private CFriendMapper friendMapper;
	@Autowired
	private CUserMapper userMapper;
	
	
	/*
	 * By MrYang
	 */
	
	//得到在线好友ID
	//private static final String GET_ONLINE_USER="select cf.FRIENDID,cu.USERTRUENAME,cp.SIMG from c_friend cf,c_user cu,c_user_hdphoto cp where cu.UID=cf.FRIENDID and cp.UID=cf.FRIENDID and cf.STATE=1 and cf.UID=?";
	private static final String GET_ONLINE_USER="select cf.FRIENDID as UID from c_friend cf,c_user_status cs where cf.FRIENDID=cs.UID and cf.STATE=1 and cs.STATUS=1 and cf.UID=?";
	public List<Map<String,Object>> getOnlineFriendList(String UID){
		return jt.queryForList(GET_ONLINE_USER,UID);
	}
	
	//更新上下线状态
	private static final String UPDATE_ONLINE_STATUS = "update c_user_status set STATUS=? where UID=?";
	public void refreshOnlineStatus(boolean online,String UID){
		if(online){
			jt.update(UPDATE_ONLINE_STATUS,1,UID);
		}else{
			jt.update(UPDATE_ONLINE_STATUS,0,UID);
		}
	}
	
	//MrYang End
	
	public AppResult passFriendAdd(HttpServletRequest request){
		AppResult r = new AppResult();
		System.out.println("添加好友");
		try {
			String UID = (String) request.getSession().getAttribute("UID");
			String userId = request.getParameter("userid");
			Map hasFriendCount = jt.queryForMap(HAS_FRIENDS, UID, userId);
			System.out.println(hasFriendCount);
			String count = hasFriendCount.get("count").toString();
			int countInt = Integer.parseInt(count);
			if (countInt < 1) {
				CFriend friends = new CFriend();
				friends.setUid(UID);
				friends.setFriendid(userId);
				friends.setState(1);
				friends.setDateline(new Date());
				friends.setCommonstate(0);
				friendMapper.insertSelective(friends);
				CFriendExample example = new CFriendExample();
				example.createCriteria().andUidEqualTo(userId).andFriendidEqualTo(UID);
				friends.setUid(userId);
				friends.setFriendid(UID);
				friendMapper.updateByExampleSelective(friends, example);
				r.setStatus(200);
				Map resultMap = new HashMap();
				resultMap.put("userId", userId);
				r.setResult(resultMap);
			} else {
				r.setStatus(201);
				Map resultMap = new HashMap();
				resultMap.put("msg", "error");
				r.setMsg("error");
				r.setResult(resultMap);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			r.setStatus(500);
			r.setMsg("error");
		}
		
		return r;
	}
	private static final String QUERY_USER_NAME_BY_UID = "SELECT USERTRUENAME FROM c_user WHERE UID = ?";

	public AppResult getFriendAddList(HttpServletRequest request) {
		System.out.println("1233333333333");
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		CFriendExample example = new CFriendExample();
		example.createCriteria().andFriendidEqualTo(UID).andStateEqualTo(0);
		List<CFriend> msgFriendList = friendMapper.selectByExample(example);
		JSONArray ja = new JSONArray();
		for (CFriend friend : msgFriendList) {
			JSONObject jo = new JSONObject();
			jo.put("friendName", jt.queryForMap(QUERY_USER_NAME_BY_UID,
					friend.getUid()).get("USERTRUENAME"));
			jo.put("friendId", friend.getUid());
			ja.add(jo);
		}
		r.setStatus(200);
		r.setResult(ja);
		r.setTotleCount(msgFriendList.size());
		return r;
	}

	private static final String HAS_FRIENDS = "SELECT count(*) as count from c_friend where UID = ? AND FRIENDID = ?";

	// 添加好友
	public AppResult addFriend(HttpServletRequest request) {
		System.out.println("添加好友");
		AppResult r = new AppResult();
		try {
			String UID = (String) request.getSession().getAttribute("UID");
			String userId = request.getParameter("userid");
			Map hasFriendCount = jt.queryForMap(HAS_FRIENDS, UID, userId);
			System.out.println(hasFriendCount);
			String count = hasFriendCount.get("count").toString();
			int countInt = Integer.parseInt(count);
			if (countInt < 1) {
				CFriend friends = new CFriend();
				friends.setUid(UID);
				friends.setFriendid(userId);
				friends.setState(0);
				friends.setDateline(new Date());
				friends.setCommonstate(0);
				friendMapper.insertSelective(friends);
				r.setStatus(200);
				Map resultMap = new HashMap();
				resultMap.put("userId", userId);
				r.setResult(resultMap);
			} else {
				r.setStatus(201);
				Map resultMap = new HashMap();
				resultMap.put("msg", "error");
				r.setMsg("error");
				r.setResult(resultMap);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			r.setStatus(500);
			r.setMsg("error");
		}

		return r;
	}
	
	private static final String GET_FRIEND_LIST = "SELECT UID,FRIENDID FROM c_friend WHERE UID = ? AND STATE = 1";
	private static final String GET_SIMG_BY_UID = "SELECT SIMG FROM c_user_hdphoto WHERE UID = ?";
	private static final String GET_USERNAME_BY_UID = "SELECT USERTRUENAME FROM c_user WHERE UID = ?";

	public String getUserName(String UID) {
		/*
		 * 抛出数组越界 By MrYang
		 */
		return (String) jt.queryForList(GET_USERNAME_BY_UID, UID).get(0).get(
				"USERTRUENAME");
	}


	// 生成地图上点
	private static final String GET_MAPID = "select MAPID from c_map_city where CITYID=?";
	private static final String GET_POINTID = "select POINTID from c_user_locate where UID=? and STATE =1";
	private static final String GET_POINT_DETAIL = "select MAPX,MAPY from c_map_activity where ACTMAPID=?";
	private static final String GET_FRIENDPHOTO = "select LIMG,MIMG,SIMG from c_user_hdphoto where UID = ?";
	private static final String GET_FRIEND_LIST_WITH_MAPID = "SELECT cf.UID,cf.FRIENDID FROM c_friend cf,c_user_locate cul WHERE cf.UID = ? AND cf.STATE = 1 and cul.MAPID=? and cul.UID = cf.UID";
	public AppResult getFriendPoint(HttpServletRequest request) {
		AppResult r = new AppResult();
		// 得到城市code
		String cityCode = request.getParameter("cityCode");

		// 得到用户UID
		String uid = (String) request.getSession().getAttribute("UID");
		// 得到城市地图ID
		Integer mapId = (Integer) jt.queryForMap(GET_MAPID, cityCode).get(
				"MAPID");
		System.out.println("mapId------------->" + mapId);
		// 得到好友列表
		List<Map<String, Object>> friendList = jt.queryForList(
				GET_FRIEND_LIST_WITH_MAPID, uid, mapId);
		ArrayList tList = new ArrayList();
		Iterator<Map<String, Object>> it = friendList.iterator();
		while (it.hasNext()) {
			Map<String, Object> next = it.next();
			// 得到该好友ID
			String friendId = (String) next.get("FRIENDID");
			// 得到该好友所在点及坐标
			List<Map<String, Object>> pointIdList = jt.queryForList(
					GET_POINTID, friendId);
			// 判断好友是否有位置信息
			if (pointIdList.size() != 0) {
				Map<String, Object> pointIdMap = pointIdList.get(0);
				Integer pointId = (Integer) pointIdMap.get("POINTID");
				Map<String, Object> pointDetailMap = jt.queryForMap(
						GET_POINT_DETAIL, pointId);
				Integer mapX = (Integer) pointDetailMap.get("MAPX");
				Integer mapY = (Integer) pointDetailMap.get("MAPY");
				// 得到好友小头像
				Map<String, Object> friendPhotoMap = jt.queryForMap(
						GET_FRIENDPHOTO, friendId);
				String friendPhoto = (String) friendPhotoMap.get("SIMG");
				next.put("friendId", friendId);
				next.put("pointId", pointId);
				next.put("mapX", mapX);
				next.put("mapY", mapY);
				next.put("friendPhoto", friendPhoto);
			} else {
				// 得到好友小头像
				Map<String, Object> friendPhotoMap = jt.queryForMap(
						GET_FRIENDPHOTO, friendId);
				String friendPhoto = (String) friendPhotoMap.get("SIMG");
				next.put("friendId", friendId);
				String noPoint = "noPoint";
				next.put("pointId", noPoint);
				next.put("mapX", noPoint);
				next.put("mapY", noPoint);
				next.put("friendPhoto", friendPhoto);
			}
			tList.add(next);
		}
		Map m = new HashMap();

		m.put("tList", tList);
		m.put("tListSize", tList.size());
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		System.out.println("r" + r);
		return r;
	}
	
	
	//得到学校好友的点
	private static final String GET_SCHOOLMAPID = "select MAPID from c_map_city where  SCHOOLID=?";
	public AppResult getSchoolFriendPoint(HttpServletRequest request) {
		AppResult r = new AppResult();
		// 得到城市code
		String schoolCode = request.getParameter("schoolCode");
		
		// 得到用户UID
		String uid = (String) request.getSession().getAttribute("UID");
		// 得到城市地图ID
		Integer mapId = (Integer) jt.queryForMap(GET_SCHOOLMAPID, schoolCode).get(
		"MAPID");
		System.out.println("mapId------------->" + mapId);
		// 得到好友列表
		List<Map<String, Object>> friendList = jt.queryForList(
				GET_FRIEND_LIST_WITH_MAPID, uid, mapId);
		ArrayList tList = new ArrayList();
		Iterator<Map<String, Object>> it = friendList.iterator();
		while (it.hasNext()) {
			Map<String, Object> next = it.next();
			// 得到该好友ID
			String friendId = (String) next.get("FRIENDID");
			// 得到该好友所在点及坐标
			List<Map<String, Object>> pointIdList = jt.queryForList(
					GET_POINTID, friendId);
			// 判断好友是否有位置信息
			if (pointIdList.size() != 0) {
				Map<String, Object> pointIdMap = pointIdList.get(0);
				Integer pointId = (Integer) pointIdMap.get("POINTID");
				Map<String, Object> pointDetailMap = jt.queryForMap(
						GET_POINT_DETAIL, pointId);
				Integer mapX = (Integer) pointDetailMap.get("MAPX");
				Integer mapY = (Integer) pointDetailMap.get("MAPY");
				// 得到好友小头像
				Map<String, Object> friendPhotoMap = jt.queryForMap(
						GET_FRIENDPHOTO, friendId);
				String friendPhoto = (String) friendPhotoMap.get("SIMG");
				next.put("friendId", friendId);
				next.put("pointId", pointId);
				next.put("mapX", mapX);
				next.put("mapY", mapY);
				next.put("friendPhoto", friendPhoto);
			} else {
				// 得到好友小头像
				Map<String, Object> friendPhotoMap = jt.queryForMap(
						GET_FRIENDPHOTO, friendId);
				String friendPhoto = (String) friendPhotoMap.get("SIMG");
				next.put("friendId", friendId);
				String noPoint = "noPoint";
				next.put("pointId", noPoint);
				next.put("mapX", noPoint);
				next.put("mapY", noPoint);
				next.put("friendPhoto", friendPhoto);
			}
			tList.add(next);
		}
		Map m = new HashMap();
		
		m.put("tList", tList);
		m.put("tListSize", tList.size());
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		System.out.println("r" + r);
		return r;
	}

	// 个人主页Friend相关内容
	private static final String GET_ATTENTCOUNT = "SELECT COUNT(FRIENDID) FROM c_friend WHERE UID = ? AND STATE = 0";

	// 得到个人主页自己的信息
	public AppResult getPersonInformation(HttpServletRequest request) {
		AppResult r = new AppResult();
		//
		String UID = request.getParameter("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIEND_LIST, UID);
		// 好友数量
		int friendCount = friendIdList.size();
		// 被关注数量
		int attentedCount = jt.queryForInt(GET_ATTENTIONCOUNT, UID);
		// 关注的人的数量
		int attentionCount = jt.queryForInt(GET_ATTENTCOUNT, UID);
		// 用户的头像
		String userPhoto = (String) jt.queryForMap(GET_FRIENDPHOTO, UID).get(
				"LIMG");
		// 用户所在省市学校
		Map<String, Object> userDetailMap = jt.queryForMap(GET_FRIEND_DETAIL,
				UID);
		String provinceName = (String) jt.queryForMap(GET_FRIEND_PROVINCE,
				userDetailMap.get("PROVINCE")).get("PROVINCESNAME");
		String cityName = (String) jt.queryForMap(GET_FRIEND_CITYNAME,
				userDetailMap.get("CITY")).get("CITYNAME");
		String schoolName = (String) jt.queryForMap(GET_FRIEND_SCHOOL,
				userDetailMap.get("SCHOOL")).get("SCHOOLNAME");
		// 用户出生年月日
		Date birth = (Date) userDetailMap.get("BIRTH");
		String birthStr = TimeUtil.DateFormat(birth);
		System.out.println(birthStr);

		// 每个好友的信息
		Iterator<Map<String, Object>> it = friendIdList.iterator();
		while (it.hasNext()) {
			Map next = it.next();
			// 好友ID
			String friendId = (String) next.get("FRIENDID");

		}
		return r;
	}

	// 个人主页Friend相关内容结束

	// 获得好友同步率
	private static final String GET_COMMONACT_COUNT = "select COUNT(a.ACTID) from (select ACTID from c_activity_attention where UID =? and STATUS=1) as a cross join (select ACTID from c_activity_attention where UID =? and STATUS=1) as b on a.ACTID=b.ACTID ";
	private static final String GET_COMMONFRIEND_COUNT = "select COUNT(a.FRIENDID) from (select FRIENDID from c_friend where STATE=1 and UID = ?) as a cross join (select FRIENDID from c_friend where STATE=1 and UID = ?)as b on a.FRIENDID = b.FRIENDID";
	private static final String SELECT_COMMONROUND_COUNT_BY_UID = "select COUNT(a.ROUNDID) from (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as a cross join (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as b on a.ROUNDID = b.ROUNDID";

	public double getCommonState(String uid, String friendId) {
		// Ma共同好友数量Ma=25%
		int Ma = jt.queryForInt(GET_COMMONFRIEND_COUNT, uid, friendId);
		// Mb共同拥有的公共圈子数量Mb=25%
		int Mb = jt.queryForInt(SELECT_COMMONROUND_COUNT_BY_UID, uid, friendId);
		// Mc共同参加过的活动数量Mc=30%
		int Mc = jt.queryForInt(GET_COMMONACT_COUNT, uid, friendId);
		// Md指定分享数量Md=20%
		int Md = 47;
		// 各项所占比例
		double Ra = 1;
		double Rb = 5;
		double Rc = 6;
		double Rd = 0.5;
		// 各项判定条件
		double Sa = Ma * Ra;
		double Sb = Mb * Rb;
		double Sc = Mc * Rc;
		double Sd = Md * Rd;
		if (Sa >= 25)
			Sa = 25;
		if (Sb >= 25)
			Sb = 25;
		if (Sc >= 30)
			Sc = 30;
		if (Sd >= 20)
			Sd = 20;
		double Eab = Sa + Sb + Sc + Sd;
		return Eab;
	}

	// 获得好友详细信息
	private static final String GET_FRIEND_DETAIL = "select * from c_user where UID = ?";
	private static final String GET_FRIEND_PROVINCE = "select PROVINCESNAME from m_provinces where CODE = ? ";
	private static final String GET_FRIEND_CITYNAME = "select CITYNAME from m_city where CODE = ?";
	private static final String GET_FRIEND_SCHOOL = "select SCHOOLNAME from m_school where SCHOOLID = ?";
	private static final String GET_FRIENDCOUNT = "select COUNT(FRIENDID) as FRIENDCOUNT from c_friend where UID = ? and state=1";
	private static final String GET_ATTENTIONCOUNT = "select COUNT(UID) as ATTENTIONCOUNT from c_friend where FRIENDID = ? and state =0";
	private static final String GET_FRIENDIDPHOTO_LIST = " SELECT cf.UID,cf.FRIENDID,cuh.SIMG FROM c_friend cf,c_user_hdphoto cuh WHERE cf.UID = ? AND STATE = 1 and cf.FRIENDID = cuh.UID";
	// 查询共同好友列表
	private static final String GET_COMMONFRIEND = "select a.* from (select FRIENDID from c_friend where STATE=1 and UID = ?) as a cross join (select FRIENDID from c_friend where STATE=1 and UID = ?)as b on a.FRIENDID = b.FRIENDID";
	private static final String GET_COMMONFRIEND_PAGING = "select a.* from (select FRIENDID from c_friend where STATE=1 and UID = ?) as a cross join (select FRIENDID from c_friend where STATE=1 and UID = ?)as b on a.FRIENDID = b.FRIENDID limit ?,?";
	private static final String GET_ATTENTIONIDPHOTO_LIST = " SELECT cf.UID,cf.FRIENDID,cuh.SIMG FROM c_friend cf,c_user_hdphoto cuh WHERE cf.FRIEND = ? AND STATE = 0 and cf.FRIENDID = cuh.UID";
	private static final String GET_FRIEND_SEX = "select USERSEX from c_user where UID =?";
	// 查询参加的活动数量
	private static final String GET_FRIENDACTIVITY_COUNT = "select COUNT(ACTID) as ACTCOUNT from c_activity_attention where UID=?";
	// 获得好友的圈子
	private static final String SELECT_ROUND_BY_UID = "select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?";
	private static final String SELECT_COMMONROUND_BY_UID = "select a.* from (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as a cross join (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as b on a.ROUNDID = b.ROUNDID";
	//获取用户一句话状态
	private static final String GET_USER_SPEAK="select DETAIL from c_user_speak where UID=?";
	
	public AppResult getFriendDetailPaging(HttpServletRequest request) {
		AppResult r = new AppResult();
		boolean compareInfo=true;
		boolean compareSchool=true;
		boolean compareStatus=true;
		boolean compareRound=true;
		String UID = (String) request.getSession().getAttribute("UID");
		String friendId = request.getParameter("friendId");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 全部数量
		
		//权限判断
		CompareUtil c = new CompareUtil();
		// 得到共同好友IDList和photoList
		List<Map<String, Object>> commonFriendList = jt.queryForList(
				GET_COMMONFRIEND_PAGING, friendId, UID,from,to);
		//System.out.println("1111111111111111111111111");
		Iterator<Map<String, Object>> it = commonFriendList.iterator();
		ArrayList commonFriendIdPhotoList = new ArrayList();
		while (it.hasNext()) {
			Map next = it.next();
			String FRIENDID = (String) next.get("FRIENDID");
			compareInfo = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_BASEINFO_LIMITS.toString());
			compareSchool = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_SCHOOLINFO_LIMITS.toString());
			compareStatus = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_MYSTATUS_LIMITS.toString());
			compareRound = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_MYROUND_LIMITS.toString());
			
			//好友姓名
			String FRIENDNAME = (String)jt.queryForMap(GET_FRIEND_NAME,FRIENDID).get("USERTRUENAME");
			String FRIENDPHOTO = (String) jt.queryForMap(GET_FRIENDPHOTO,
					FRIENDID).get("SIMG");
			// 获得好友同步率
			double commonState = getCommonState(UID, FRIENDID);
			//获得一句话介绍
			String userStatu = (String) jt.queryForMap(GET_USER_SPEAK,FRIENDID).get("DETAIL");
			// 分别获得好友的省市学校
			Map<String, Object> friendDetailMap = jt.queryForMap(GET_FRIEND_DETAIL,
					FRIENDID);
			String provinceName = (String) jt.queryForMap(GET_FRIEND_PROVINCE,
					friendDetailMap.get("PROVINCE")).get("PROVINCESNAME");
			String cityName = (String) jt.queryForMap(GET_FRIEND_CITYNAME,
					friendDetailMap.get("CITY")).get("CITYNAME");
			String schoolName = (String) jt.queryForMap(GET_FRIEND_SCHOOL,
					friendDetailMap.get("SCHOOL")).get("SCHOOLNAME");
			// 得到用户最近参加的活动
			List<Map<String, Object>> friendActivityList = jt.queryForList(
					SELECT_NEAR_ACTIVITY, FRIENDID);
			String friendActivityName1;
			String friendActivityName2 = null;
			if (friendActivityList.size() != 0) {
				if(friendActivityList.size()<1){
					friendActivityName1 = (String) friendActivityList.get(0).get("TITLE");
					friendActivityName2 = null;
				}
				else{
					friendActivityName1 = (String) friendActivityList.get(0).get("TITLE");
					friendActivityName2 = (String) friendActivityList.get(1).get("TITLE");
				}
			} 
			else {
				friendActivityName1 = "此人近期没有参加任何活动";
			}
			// 得到共同好友IDList和photoList
			List<Map<String, Object>> commonFriendCommonList = jt.queryForList(
					GET_COMMONFRIEND, FRIENDID, UID);
			Iterator<Map<String, Object>> itt = commonFriendCommonList.iterator();
			//得到共同好友的共同好友的姓名列表
			ArrayList commonCommonFriendIdNameList = new ArrayList();
			while (itt.hasNext()) {
				Map m = itt.next();
				//好友姓名
				String COMMONFRIENDID = (String) m.get("FRIENDID");
				String FRIENDNAMECommon = (String)jt.queryForMap(GET_FRIEND_NAME,COMMONFRIENDID).get("USERTRUENAME");
				m.put("FRIENDNAMECommon", FRIENDNAMECommon);
				m.put("COMMONFRIENDID", COMMONFRIENDID);
				System.out.println(m);
				commonCommonFriendIdNameList.add(m);
			}
			System.out.println("commonCommonFriendIdNameList===========>"+commonCommonFriendIdNameList);
			// 获得好友共同圈子
			List<Map<String, Object>> friendCommonRoundList = jt.queryForList(
					SELECT_COMMONROUND_BY_UID, UID, FRIENDID);
			
			
			//基本信息隐私
			if(!compareInfo){
				provinceName=Constants.LIMIT_CANNOT;
				cityName="";
			}
			//学校隐私
			if(!compareSchool){
				schoolName="";
			}
			//一句话状态隐私
			if(!compareStatus){
				userStatu=Constants.LIMIT_CANNOT;
			}
			
			System.out.println("compareInfo"+compareInfo);
			next.put("friendActivityListSize", friendActivityList.size());
			next.put("FRIENDPHOTO", FRIENDPHOTO);
			next.put("FRIENDID", FRIENDID);
			next.put("FRIENDNAME", FRIENDNAME);
			next.put("friendCommonRoundList", friendCommonRoundList);
			next.put("friendCommonRoundListSize", friendCommonRoundList.size());
			System.out.println(next);
			next.put("commonState", commonState);
			next.put("userStatu", userStatu);
			next.put("provinceName", provinceName);
			next.put("schoolName", schoolName);
			next.put("cityName", cityName);
			System.out.println(next);
			next.put("friendActivityName1", friendActivityName1);
			next.put("friendActivityName2", friendActivityName2);
			next.put("commonCommonFriendIdNameList", commonCommonFriendIdNameList);
			next.put("commonCommonFriendIdNameListSize", commonCommonFriendIdNameList.size());
			System.out.println("end----->"+next);
			
			System.out.println(next);
			commonFriendIdPhotoList.add(next);
		}
		System.out.println(commonFriendIdPhotoList);
		int All_record = jt.queryForInt(GET_COMMONFRIEND_COUNT, UID, friendId);;
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("commonFriendIdPhotoList", commonFriendIdPhotoList);
		m.put("commonFriendIdPhotoListSize", commonFriendIdPhotoList.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	//得到共同好友主页分页内容
	public AppResult getCommonFriendDetail(HttpServletRequest request) {
		AppResult r = new AppResult();
		boolean compareInfo=true;
		boolean compareSchool=true;
		boolean compareStatus=true;
		boolean compareRound=true;
		
		String friendId = request.getParameter("friendId");
		String UID = (String) request.getSession().getAttribute("UID");
		
		CompareUtil c = new CompareUtil();
		compareInfo = c.compare(jt,UID,friendId, LimitType.COMPARE_BASEINFO_LIMITS.toString());
		compareSchool = c.compare(jt,UID,friendId, LimitType.COMPARE_SCHOOLINFO_LIMITS.toString());
		compareStatus = c.compare(jt,UID,friendId, LimitType.COMPARE_MYSTATUS_LIMITS.toString());
		compareRound = c.compare(jt,UID,friendId, LimitType.COMPARE_MYROUND_LIMITS.toString());
		// 获得好友同步率
		double commonState = getCommonState(UID, friendId);
		//获得一句话介绍
		String userStatu = (String) jt.queryForMap(GET_USER_SPEAK,friendId).get("DETAIL");
		// 获得好友的圈子
		List<Map<String, Object>> friendRoundList = jt.queryForList(
				SELECT_ROUND_BY_UID, friendId);
		// 获得好友共同圈子
		List<Map<String, Object>> friendCommonRoundList = jt.queryForList(
				SELECT_COMMONROUND_BY_UID, UID, friendId);
		// 获得好友头像
		Map<String, Object> friendPhotoMap = jt.queryForMap(GET_FRIENDPHOTO,
				friendId);
		String photo = (String) friendPhotoMap.get("LIMG");
		// 获得好友姓名
		String friendName = (String) jt.queryForMap(GET_FRIEND_NAME, friendId)
		.get("USERTRUENAME");
		System.out.println("friendName" + friendName);
		// 获得好友性别
		Integer friendSex = Integer.valueOf((String) jt.queryForMap(
				GET_FRIEND_SEX, friendId).get("USERSEX"));
		// 获得好友参加活动数量
		System.out.println("friendSex" + friendSex);
		// 分别获得好友的省市学校
		Map<String, Object> friendDetailMap = jt.queryForMap(GET_FRIEND_DETAIL,
				friendId);
		String provinceName = (String) jt.queryForMap(GET_FRIEND_PROVINCE,
				friendDetailMap.get("PROVINCE")).get("PROVINCESNAME");
		String cityName = (String) jt.queryForMap(GET_FRIEND_CITYNAME,
				friendDetailMap.get("CITY")).get("CITYNAME");
		String schoolName = (String) jt.queryForMap(GET_FRIEND_SCHOOL,
				friendDetailMap.get("SCHOOL")).get("SCHOOLNAME");
		
		
		// 得到用户最近参加的活动
		List<Map<String, Object>> friendActivityList = jt.queryForList(
				SELECT_NEAR_ACTIVITY, friendId);
		String friendActivityName;
		if (friendActivityList.size() != 0) {
			friendActivityName = (String) friendActivityList.get(0)
			.get("TITLE");
		} else {
			friendActivityName = "此人近期没有参加任何活动";
		}
		
		// 得到用户好友的ID和photoList
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDIDPHOTO_LIST, friendId);
		
		// 得到共同好友IDList和photoList
		List<Map<String, Object>> commonFriendList = jt.queryForList(
				GET_COMMONFRIEND, friendId, UID);
		Iterator<Map<String, Object>> it = commonFriendList.iterator();
		ArrayList commonFriendIdPhotoList = new ArrayList();
		while (it.hasNext()) {
			Map next = it.next();
			String FRIENDID = (String) next.get("FRIENDID");
			//好友姓名
			String FRIENDNAME = (String)jt.queryForMap(GET_FRIEND_NAME,FRIENDID).get("USERTRUENAME");
			String FRIENDPHOTO = (String) jt.queryForMap(GET_FRIENDPHOTO,
					FRIENDID).get("SIMG");
			next.put("FRIENDPHOTO", FRIENDPHOTO);
			next.put("FRIENDID", FRIENDID);
			next.put("FRIENDNAME", FRIENDNAME);
			System.out.println("commonFriendIdPhotoList---------!---------"
					+ next);
			commonFriendIdPhotoList.add(next);
		}
		//基本信息隐私
		if(!compareInfo){
			provinceName=Constants.LIMIT_CANNOT;
			cityName="";
		}
		//学校隐私
		if(!compareSchool){
			schoolName="";
		}
		//一句话状态隐私
		if(!compareStatus){
			userStatu=Constants.LIMIT_CANNOT;
		}
		//一句话状态隐私
		if(!compareRound){
			friendRoundList.clear();
			Map m = new HashMap();
			m.put("", Constants.LIMIT_CANNOT);
		}
		Map m = new HashMap();
		m.put("userStatu", userStatu);
		m.put("friendRoundList", friendRoundList);
		m.put("friendRoundListSize", friendRoundList.size());
		m.put("commonState", commonState);
		m.put("friendCommonRoundList", friendCommonRoundList);
		m.put("friendCommonRoundListSize", friendCommonRoundList.size());
		m.put("friendIdList", friendIdList);
		m.put("friendIdListSize", friendIdList.size());
		m.put("commonFriendIdPhotoList", commonFriendIdPhotoList);
		m.put("commonFriendIdPhotoListSize", commonFriendIdPhotoList.size());
		m.put("friendId", friendId);
		m.put("friendActCount", jt.queryForMap(GET_FRIENDACTIVITY_COUNT,
				friendId).get("ACTCOUNT"));
		m.put("friendName", friendName);
		m.put("friendSex", friendSex);
		m.put("photo", photo);
		m.put("provinceName", provinceName);
		m.put("cityName", cityName);
		m.put("schoolName", schoolName);
		m.put("friendCount", jt.queryForMap(GET_FRIENDCOUNT, friendId).get(
		"FRIENDCOUNT"));
		m.put("attentionCount", jt.queryForMap(GET_ATTENTIONCOUNT, friendId)
				.get("ATTENTIONCOUNT"));
		m.put("friendActivityName", friendActivityName);
		System.out.println("m!!!!!!!!!!!!!!!!!!!!!!!!!!!" + m);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	//查询用户分页
	private static final String SEARCH_USER_BY_NAME = "select UID,USERTRUENAME from c_user where USERTRUENAME like ? limit ?,?";
	//查询用户数目
	private static final String SEARCH_USER_BY_NAME_COUNT = "select count(UID) from c_user where USERTRUENAME like ? ";
	//查询获得用户按分页排列
	public AppResult searchUser(HttpServletRequest request){
		AppResult r = new AppResult();
		String userNameStr = "%"+request.getParameter("userName")+"%"; 
		String UID = (String) request.getSession().getAttribute("UID");
		boolean compareInfo=true;
		boolean compareSchool=true;
		boolean compareStatus=true;
		boolean compareRound=true;
		CompareUtil c = new CompareUtil();
		//分页
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		// 得到共同好友IDList和photoList
		List<Map<String, Object>> searchUserList = jt.queryForList(
				SEARCH_USER_BY_NAME, userNameStr,from,to);
		System.out.println(searchUserList);
		Iterator<Map<String, Object>> it = searchUserList.iterator();
		ArrayList searchUserListPaging = new ArrayList();
		while (it.hasNext()) {
			Map next = it.next();
			String FRIENDID = (String) next.get("UID");
			compareInfo = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_BASEINFO_LIMITS.toString());
			compareSchool = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_SCHOOLINFO_LIMITS.toString());
			compareStatus = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_MYSTATUS_LIMITS.toString());
			compareRound = c.compare(jt,UID,FRIENDID, LimitType.COMPARE_MYROUND_LIMITS.toString());
			
			//好友姓名
			String FRIENDNAME = (String)jt.queryForMap(GET_FRIEND_NAME,FRIENDID).get("USERTRUENAME");
			String FRIENDPHOTO = (String) jt.queryForMap(GET_FRIENDPHOTO,
					FRIENDID).get("SIMG");
			// 获得好友同步率
			double commonState = getCommonState(UID, FRIENDID);
			//获得一句话介绍
			String userStatu = (String) jt.queryForMap(GET_USER_SPEAK,FRIENDID).get("DETAIL");
			// 分别获得好友的省市学校
			Map<String, Object> friendDetailMap = jt.queryForMap(GET_FRIEND_DETAIL,
					FRIENDID);
			String provinceName = (String) jt.queryForMap(GET_FRIEND_PROVINCE,
					friendDetailMap.get("PROVINCE")).get("PROVINCESNAME");
			String cityName = (String) jt.queryForMap(GET_FRIEND_CITYNAME,
					friendDetailMap.get("CITY")).get("CITYNAME");
			String schoolName = (String) jt.queryForMap(GET_FRIEND_SCHOOL,
					friendDetailMap.get("SCHOOL")).get("SCHOOLNAME");
			// 得到用户最近参加的活动
			List<Map<String, Object>> friendActivityList = jt.queryForList(
					SELECT_NEAR_ACTIVITY, FRIENDID);
			String friendActivityName1;
			String friendActivityName2 = null;
			if (friendActivityList.size() != 0) {
				if(friendActivityList.size()<1){
					friendActivityName1 = (String) friendActivityList.get(0).get("TITLE");
					friendActivityName2 = null;
				}
				else{
					friendActivityName1 = (String) friendActivityList.get(0).get("TITLE");
					friendActivityName2 = (String) friendActivityList.get(1).get("TITLE");
				}
			} 
			else {
				friendActivityName1 = "此人近期没有参加任何活动";
			}
			// 得到共同好友IDList和photoList
			List<Map<String, Object>> commonFriendCommonList = jt.queryForList(
					GET_COMMONFRIEND, FRIENDID, UID);
			Iterator<Map<String, Object>> itt = commonFriendCommonList.iterator();
			//得到共同好友的共同好友的姓名列表
			ArrayList commonCommonFriendIdNameList = new ArrayList();
			while (itt.hasNext()) {
				Map m = itt.next();
				//好友姓名
				String COMMONFRIENDID = (String) m.get("FRIENDID");
				String FRIENDNAMECommon = (String)jt.queryForMap(GET_FRIEND_NAME,COMMONFRIENDID).get("USERTRUENAME");
				m.put("FRIENDNAMECommon", FRIENDNAMECommon);
				m.put("COMMONFRIENDID", COMMONFRIENDID);
				System.out.println(m);
				commonCommonFriendIdNameList.add(m);
			}
			System.out.println("commonCommonFriendIdNameList===========>"+commonCommonFriendIdNameList);
			// 获得好友共同圈子
			List<Map<String, Object>> friendCommonRoundList = jt.queryForList(
					SELECT_COMMONROUND_BY_UID, UID, FRIENDID);
			
			
			//基本信息隐私
			if(!compareInfo){
				provinceName=Constants.LIMIT_CANNOT;
				cityName="";
			}
			//学校隐私
			if(!compareSchool){
				schoolName="";
			}
			//一句话状态隐私
			if(!compareStatus){
				userStatu=Constants.LIMIT_CANNOT;
			}
			
			System.out.println("compareInfo"+compareInfo);
			next.put("friendActivityListSize", friendActivityList.size());
			next.put("FRIENDPHOTO", FRIENDPHOTO);
			next.put("FRIENDID", FRIENDID);
			next.put("FRIENDNAME", FRIENDNAME);
			next.put("friendCommonRoundList", friendCommonRoundList);
			next.put("friendCommonRoundListSize", friendCommonRoundList.size());
			System.out.println(next);
			next.put("commonState", commonState);
			next.put("userStatu", userStatu);
			next.put("provinceName", provinceName);
			next.put("schoolName", schoolName);
			next.put("cityName", cityName);
			System.out.println(next);
			next.put("friendActivityName1", friendActivityName1);
			next.put("friendActivityName2", friendActivityName2);
			next.put("commonCommonFriendIdNameList", commonCommonFriendIdNameList);
			next.put("commonCommonFriendIdNameListSize", commonCommonFriendIdNameList.size());
			System.out.println("end----->"+next);
			
			System.out.println(next);
			searchUserListPaging.add(next);
		}
		
		int All_record = jt.queryForInt(SEARCH_USER_BY_NAME_COUNT,userNameStr);
		System.out.println(All_record+"============All_record");
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("searchUserListPaging", searchUserListPaging);
		m.put("searchUserListPagingSize", searchUserListPaging.size());
		m.put("All_page", All_page);
		m.put("All_record", All_record);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	
	
	// 获得点上的好友列表
	private static final String GET_POINT_FRIEND = "select UID from c_user_locate where POINTID=? and state =1 ";
	private static final String GET_FRIEND_NAME = "select USERTRUENAME from c_user where UID = ?";

	public AppResult getPointFriend(HttpServletRequest request) {
		AppResult r = new AppResult();
		boolean compareStatus=true;
		CompareUtil c = new CompareUtil();
		
		String pointId = request.getParameter("pointId");
		String uid = (String) request.getSession().getAttribute("UID");
		
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_POINT_FRIEND, pointId);
		Iterator<Map<String, Object>> it = friendIdList.iterator();
		ArrayList tList = new ArrayList();
		while (it.hasNext()) {
			Map<String, Object> next = it.next();
			String friendId = (String) next.get("UID");
			double commonState = getCommonState(uid, friendId);
			//获得一句话介绍
			String userStatu = (String) jt.queryForMap(GET_USER_SPEAK,friendId).get("DETAIL");
			// 得到用户最近参加的活动
			List<Map<String, Object>> friendActivityList = jt.queryForList(
					SELECT_NEAR_ACTIVITY, friendId);
			String friendActivityName;
			if (friendActivityList.size() != 0) {
				friendActivityName = (String) friendActivityList.get(0).get(
						"TITLE");
			} else {
				friendActivityName = "此人近期没有参加任何活动";
			}

			// 得到好友头像
			Map<String, Object> friendPhotoMap = jt.queryForMap(
					GET_FRIENDPHOTO, friendId);
			String friendPhoto = (String) friendPhotoMap.get("MIMG");

			// 得到好友姓名
			Map<String, Object> friendNameMap = jt.queryForMap(GET_FRIEND_NAME,
					friendId);
			String friendName = (String) friendNameMap.get("USERTRUENAME");
			compareStatus = c.compare(jt,uid,friendId, LimitType.COMPARE_MYSTATUS_LIMITS.toString());
			if(!compareStatus){
				userStatu=Constants.LIMIT_CANNOT;
			}
			next.put("friendId", friendId);
			next.put("friendActivityName", friendActivityName);
			next.put("friendPhoto", friendPhoto);
			next.put("friendName", friendName);
			next.put("commonState", commonState);
			next.put("userStatu", userStatu);
			tList.add(next);
		}
		Map m = new HashMap();
		m.put("tList", tList);
		m.put("tListSize", tList.size());
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}

	// 得到好友所有信息,state为1查询我的好友ID，0为好友为我的人的ID
	public Map getAllFriendList(List<Map<String, Object>> friendIdList,
			int state, String UID) {
		System.out.println(friendIdList);
		List<Map<String, Object>> allList = new ArrayList<Map<String, Object>>();
		Iterator<Map<String, Object>> it = friendIdList.iterator();
		//判断是用户是否+隐私
		boolean compareTip=true;
		CompareUtil c = new CompareUtil();
		
		while (it.hasNext()) {
			
			// 得到这个人作为NEXT
			Map<String, Object> next = it.next();

			// 得到friendId
			String friendId;
			if (state == 1) {
				friendId = (String) next.get("FRIENDID");
			} else {
				friendId = (String) next.get("UID");
			}
			// 得到同步率
			System.out.println(next.get("COMMONSTATE"));
			double commonState = getCommonState(UID, friendId);
			System.out.println("commonState-->" + commonState);
			System.out.println("friendId---->" + friendId);
			Map<String, Object> friendNameMap = jt.queryForMap(GET_FRIENDNAME,
					friendId);

			// 得到用户姓名
			String friendName = (String) friendNameMap.get("USERTRUENAME");

			// 得到用户头像
			Map<String, Object> friendPhotoMap = jt.queryForMap(
					GET_FRIENDPHOTO, friendId);
			String friendPhoto = (String) friendPhotoMap.get("MIMG");

			// 得到用户性别
			int userSexInt = Integer.valueOf((String) friendNameMap
					.get("USERSEX"));

			// 得到用户最近参加的活动
			List<Map<String, Object>> friendActivityList = jt.queryForList(
					SELECT_NEAR_ACTIVITY, friendId);
			String friendActivityName;
			if (friendActivityList.size() != 0) {
				friendActivityName = (String) friendActivityList.get(0).get(
						"TITLE");
			} else {
				friendActivityName = "此人近期没有参加任何活动";
			}

			// 得到人的TIPNAME
			List<Map<String, Object>> friendTipNameList = jt.queryForList(
					GET_TIPNAME, friendId);
			compareTip=c.compare(jt, UID, friendId, LimitType.COMPARE_MYTIPS_LIMITS.toString());
			if(!compareTip){
				friendTipNameList.clear();
				Map m = new HashMap();
				m.put("TIPNAME", Constants.LIMIT_CANNOT);
				friendTipNameList.add(m);
			}
			// 将用户信息放入List里
			next.put("friendId", friendId);
			next.put("friendPhoto", friendPhoto);
			next.put("commonState", commonState);
			next.put("friendName", friendName);
			System.out.println("friendName-->" + friendName);
			next.put("userSexInt", userSexInt);
			next.put("friendActivityName", friendActivityName);
			next.put("friendTipNameList", friendTipNameList);
			next.put("friendTipNameListSize", friendTipNameList.size());
			System.out.println("next-->" + next);
			System.out.println("friendPhoto-->" + friendPhoto);
			allList.add(next);
		}
		Map map = new HashMap();
		map.put("allList", allList);
		map.put("allListSize", allList.size());
		return map;

	}
	//得到下面好友详细以分页形式显示
	private static final String GET_FRIENDS_PAGING_BOTTOM="select cf.COMMONSTATE,USERSEX,PROVINCESNAME,CITYNAME,SCHOOLNAME,cu.USERTRUENAME,FRIENDID,SIMG from m_school ms,c_user_hdphoto cuh,c_friend cf,c_user cu,m_provinces mp,m_city mc where cu.UID=cf.FRIENDID and cu.PROVINCE =mp.CODE and cu.CITY=mc.CODE and cu.SCHOOL = ms.SCHOOLID and cf.STATE=1  and cf.FRIENDID=cuh.UID and cf.UID=? limit ?,?";
	public AppResult getFiendPagingBottom(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String personFriendId=request.getParameter("personFriendId");
		String userId = (String) request.getSession().getAttribute("UID");
		if(personFriendId!=null){
			userId=personFriendId;
		}
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING_BOTTOM,userId,from,to);
		//List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	//得到我关注的好友详细以分页形式显示
	private static final String GET_FRIENDS_PAGING_BOTTOM_ATTENTION="select cf.COMMONSTATE,USERSEX,PROVINCESNAME,CITYNAME,SCHOOLNAME,cu.USERTRUENAME,FRIENDID,SIMG from m_school ms,c_user_hdphoto cuh,c_friend cf,c_user cu,m_provinces mp,m_city mc where cu.UID=cf.FRIENDID and cf.STATE=0 and cu.PROVINCE =mp.CODE and cu.CITY=mc.CODE and cu.SCHOOL = ms.SCHOOLID  and cf.FRIENDID=cuh.UID and cf.UID=? limit ?,?";
	public AppResult getFiendPagingBottomAttention(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String userId = (String) request.getSession().getAttribute("UID");
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING_BOTTOM_ATTENTION,userId,from,to);
	//	List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	
	//得到关注我的好友详细以分页形式显示
	private static final String GET_FRIENDS_PAGING_BOTTOM_ATTENTED="select cf.COMMONSTATE,USERSEX,PROVINCESNAME,CITYNAME,SCHOOLNAME,cu.USERTRUENAME,FRIENDID,SIMG from m_school ms,c_user_hdphoto cuh,c_friend cf,c_user cu,m_provinces mp,m_city mc where cu.UID=cf.UID and cf.STATE=0 and cu.PROVINCE =mp.CODE and cu.CITY=mc.CODE and cu.SCHOOL = ms.SCHOOLID  and cf.UID=cuh.UID and cf.FRIENDID=? limit ?,?";
	public AppResult getFiendPagingBottomAttented(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String userId = (String) request.getSession().getAttribute("UID");
		String personFriendId=request.getParameter("personFriendId");
		if(personFriendId!=null){
			userId=personFriendId;
		}
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING_BOTTOM_ATTENTED,userId,from,to);
		//List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	
	private static final String GET_FRIENDS_PAGING_WITH_SEX="select cf.FRIENDID,cuh.SIMG from c_user_hdphoto cuh,c_friend cf,c_user cu where cu.UID=cf.FRIENDID and cf.FRIENDID=cuh.UID and cf.STATE=1 and cf.UID=? and cu.USERSEX=? limit ?,? ";
	public AppResult getFriendPagingWithSex(HttpServletRequest request){
		AppResult r = new AppResult();
		String userSex = request.getParameter("userSex");
		String homePersonId = request.getParameter("homePersonId");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("homePersonId---->"+homePersonId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList = jt.queryForList(GET_FRIENDS_PAGING_WITH_SEX,homePersonId,userSex,from,to);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	
	//得到好友以分页形式显示
	private static final String GET_FRIENDS_PAGING="select FRIENDID,SIMG from c_user_hdphoto cuh,c_friend cf where  cf.FRIENDID=cuh.UID and cf.STATE=1 and cf.UID=? limit ?,?";
	
	public AppResult getFriendPaging(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String homePersonId = request.getParameter("homePersonId");
		System.out.println("homePersonId------------------------------------->"+homePersonId);
		String userId = (String) request.getSession().getAttribute("UID");
		if(homePersonId!=null){
			userId=homePersonId;
		}
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING,userId,from,to);
		//List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	
	//得到在线好友以分页形式显示
	private static final String GET_FRIENDS_PAGING_OL="select FRIENDID,SIMG,cus.STATUS from c_user_hdphoto cuh,c_friend cf,c_user_status cus where cus.UID=cf.FRIENDID and cf.STATE=1 and cus.STATUS=1  and cf.FRIENDID=cuh.UID and cf.UID=? limit ?,?";
	
	public AppResult getFriendPagingOL(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		String Sort = request.getParameter("Sort");
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String userId = (String) request.getSession().getAttribute("UID");
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING_OL,userId,from,to);
		//List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	//得到在线好友以分页形式显示
	private static final String GET_FRIENDS_PAGING_COMMON="select FRIENDID,SIMG from c_user_hdphoto cuh,c_friend cf  where cf.FRIENDID=cuh.UID and cf.STATE=1 and cf.UID=? order by cf.COMMONSTATE DESC limit ?,?";
	
	public AppResult getFriendPagingCommon(HttpServletRequest request){
		AppResult r = new AppResult();
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		String Sort = request.getParameter("Sort");
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));

		String userId = (String) request.getSession().getAttribute("UID");
		
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		System.out.println("userId---->"+userId);
		System.out.println("from---->"+from);
		System.out.println("to---->"+to);
		List<Map<String, Object>> friendPagingList=jt.queryForList(GET_FRIENDS_PAGING_COMMON,userId,from,to);
		//List<Map<String, Object>> friendIdList = jt.queryForList(GET_FRIEND_LIST, userId);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	//得到搜索的好友以分页形式展现
	private static final String SEARCH_FRIEND_WITH_NAME="select cf.COMMONSTATE,USERSEX,PROVINCESNAME,CITYNAME,SCHOOLNAME,cu.USERTRUENAME,FRIENDID,SIMG from m_school ms,c_user_hdphoto cuh,c_friend cf,c_user cu,m_provinces mp,m_city mc where  cu.PROVINCE =mp.CODE and cu.CITY=mc.CODE and cu.SCHOOL = ms.SCHOOLID and cf.STATE=1  and cf.UID=cuh.UID and cf.UID=cu.UID  and cf.FRIENDID=? and cu.USERTRUENAME like ? limit ?,?";
	public AppResult getSearchPersonFriend(HttpServletRequest request){
		AppResult r = new AppResult();
		String friendName = request.getParameter("friendName");
		Integer PageCount=Integer.valueOf(request.getParameter("PageCount"));
		String Sort = request.getParameter("Sort");
		Integer Current_page = Integer.valueOf(request.getParameter("Current_page"));
		String userId = (String) request.getSession().getAttribute("UID");
		//从第几条
		int from=0+(Current_page-1)*PageCount;
		//到第几条
		int to = from+PageCount;
		//得到这一页的全部
		
		String friendNameStr = "%"+friendName+"%";
		System.out.println(friendNameStr);
		List<Map<String, Object>> friendPagingList = jt.queryForList(SEARCH_FRIEND_WITH_NAME,userId,friendNameStr,from,to);
		// 全部数量
		int All_record = friendPagingList.size();
		//全部页数
		int All_page=0;
		if(All_record%PageCount==0){
			All_page =All_record/PageCount ;
		}
		else{
			All_page =All_record/PageCount+1;
		}
		
		Map m = new HashMap();
		m.put("friendPagingList", friendPagingList);
		m.put("friendPagingListSize", friendPagingList.size());
		m.put("All_record", All_record);
		m.put("All_page", All_page);
		r.setResult(m);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}
	//显示个人中好友详细
	public AppResult getPersonFriendDetail(HttpServletRequest request){
		AppResult r = new AppResult();
		String friendId = request.getParameter("friendId");
		String userId = (String) request.getSession().getAttribute("UID");
		//共同好友数量
		int commonFriendsCount=jt.queryForList(GET_COMMONFRIEND,userId,friendId).size();
		//获得好友同步率
		double commonState = getCommonState(userId, friendId);
		String commonStateStr = commonState+"%";
		//好友头像
		String userPhoto = (String) jt.queryForMap(GET_FRIENDPHOTO,friendId).get("SIMG");
		//好友姓名
		String userName = (String)jt.queryForMap(GET_FRIEND_NAME,friendId).get("USERTRUENAME");
		// 共同参加过的活动数量
		int commonActivity = jt.queryForInt(GET_COMMONACT_COUNT, userId, friendId);
		// 共同拥有的公共圈子数量
		int commonRound = jt.queryForInt(SELECT_COMMONROUND_COUNT_BY_UID, userId, friendId);
		Map m = new HashMap();
		m.put("commonFriendsCount", commonFriendsCount);
		m.put("commonStateStr", commonStateStr);
		m.put("commonState", commonState);
		m.put("userPhoto", userPhoto);
		m.put("userName", userName);
		m.put("commonActivity", commonActivity);
		m.put("commonRound", commonRound);
		r.setResult(m);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}
	
	
	// 得到好友以同步率排序
	private static final String GET_FRIENDLIST_WITH_COMMONSTATE = "select FRIENDID,COMMONSTATE from c_friend WHERE STATE=1 AND UID = ? order by COMMONSTATE ";
	private static final String GET_TIPNAME = "select TIPNAME from c_tip ct, c_user_tip cut where ct.TIPID =cut.TIP and cut.UID = ?";
	private static final String GET_FRIENDNAME = "select USERTRUENAME,USERSEX from c_user where UID = ?";
	private static final String SELECT_NEAR_ACTIVITY = "select TITLE,a.ACTID from c_activity a,c_activity_attention caa where caa.ACTID=a.ACTID and caa.status = 1 and caa.UID = ? and (SELECT TIMEDIFF(now(),a.BEGINTIME) < 72) order by a.BEGINTIME DESC limit 0,1";
	private static final String GET_USERPHOTO = "select LIMG,SIMG,MIMG from c_user_hdphoto where UID=? ";

	public AppResult getCommonStateFriendList(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDLIST_WITH_COMMONSTATE, UID);
		Map allFriendListMap = getAllFriendList(friendIdList, 1, UID);
		r.setResult(allFriendListMap);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	// 得到我关注的人
	private static final String GET_FRIENDLIST_WITH_ATTENTION = "select FRIENDID,COMMONSTATE from c_friend WHERE STATE=0 AND UID = ? order by COMMONSTATE";

	public AppResult getAttentionFriendList(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDLIST_WITH_ATTENTION, UID);
		Map allFriendListMap = getAllFriendList(friendIdList, 1, UID);
		r.setResult(allFriendListMap);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	// 得到关注我的人
	private static final String GET_FRIENDLIST_WITH_ATTENTED = "select UID,COMMONSTATE from c_friend WHERE STATE=0 AND FRIENDID = ? order by COMMONSTATE";

	public AppResult getAttentedFriendList(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDLIST_WITH_ATTENTED, UID);
		Map allFriendListMap = getAllFriendList(friendIdList, 0, UID);
		r.setResult(allFriendListMap);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	// 得到在线好友
	private static final String GET_FRIENDLIST_WITH_ONLINE = "select cus.UID,cf.COMMONSTATE from c_friend cf,c_user_status cus where cf.STATE=1 and cf.FRIENDID=cus.UID and cf.UID = ?  and cus.STATUS=1  order by COMMONSTATE";

	public AppResult getOlineFriendList(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDLIST_WITH_ONLINE, UID);
		Map allFriendListMap = getAllFriendList(friendIdList, 0, UID);
		r.setResult(allFriendListMap);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	// 得到不在线好友
	private static final String GET_FRIENDLIST_WITH_UNONLINE = "select cus.UID,cf.COMMONSTATE from c_friend cf,c_user_status cus where cf.STATE=1 and cf.FRIENDID=cus.UID and cf.UID = ? and cus.STATUS not between 1 and 1  order by COMMONSTATE";

	public AppResult getUnolineFriendList(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		List<Map<String, Object>> friendIdList = jt.queryForList(
				GET_FRIENDLIST_WITH_UNONLINE, UID);
		Map allFriendListMap = getAllFriendList(friendIdList, 0, UID);
		r.setResult(allFriendListMap);
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}


	private static final String QUERY_FRIEND_PAGE = "SELECT * FROM c_friend f ,c_user u where f.UID = u.UID WHERE f.UID = ? ";

	public PagingList getFriendPageList(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return getPagingList(QUERY_FRIEND_PAGE, request);
	}

}
